package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.BusinessModeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author zhouJiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2022年3月25日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(BusinessModeAttrHandler.class)
public class BusinessModeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String attrValue = attr.getValue();
        String sceneInstId = data.getSceneInstId();

        // 主框架业务模式与子框架的联动处理
        setNodeBandWidthMode(attrValue, inst, sceneInstId);
    }
    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private void setNodeBandWidthMode(String value, KiteInst inst, String sceneInstId) {
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        kiteProdInstList.stream().forEach(prodInst ->{
            // 业务模式
            KiteAttr kiteAttr = prodInst.getAttr("2_0000003");
            if (Objects.nonNull(kiteAttr) && !KiteStringUtils.isEqual(inst.getInstId(), prodInst.getProdInstId())) {
                List<KiteAttrValue> attrValues = kiteAttr.getAttrValues();
                if (KiteListUtils.isNotEmpty(attrValues)) {
                    if("10003509".equals(prodInst.getSpecId()) || "63249780".equals(prodInst.getSpecId())){ // 10003509为云联网专线接入产品-新版的prodId
                        operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), value, sceneInstId, null);
                    }
                }
            }
        });
    }
}
